<?php

								/**
								 * @author Stan Bordeaux
								 * @copyright 2011
								 */
								session_start();
								//check for form submission
								if (isset($_POST['Submit']))
								{
									$pword = trim($_POST['pword']);
									$pcheck = trim($_POST['cpword']);
									$username = trim($_POST['username']);
									$email = trim($_POST['email']);
									$found = false;
									
									//for security check that the passwords match
									if ($pword !== $pcheck)
									{
										$result = "Your passwords don't match";
									}
									else
									{
										//all ok so far lets open the members file
										$members_file = 'members.txt';
										$file = fopen($members_file, 'a+');
									  
										//if the file is empty just write to it
										if (filesize($members_file) === 0)
										{
											fwrite($file, "$username, $email, $pword");
										}
										else
										{
										
											rewind($file);
											while (!feof($file))
											{
												$line = fgets($file);
												$temp = explode(', ', $line);
												if ($temp[0] == $username)
												{
													$found = true;
													
													break;
												}
											}
										}
										
										//if the username is unique go ahead and write data to file
										
										if (!$found)
										{
											//add the contents to the file
											fwrite($file, "\r\n$username, $email, $pword");
											
										
											fclose($file);
											
											$_SESSION['message'] = "<p><span style='color: green; font-weight: bold;'>Registration complete. Login to enter members area</span></p>";
											header('Location: http://localhost/nanocms/index.php');
										}
										else
										{
											$_SESSION['message'] = "Username already taken. Choose another one.";
											header('Location: http://localhost/nanocms/register.php');

										}
										
									}
								}
								?>